package com.mall.user.service.impl;

import com.mall.user.model.Address;
import com.mall.user.mapper.AddressMapper;
import com.mall.user.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 收件信息表 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-08-29
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addAddress(Address address){
        return super.save(address);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(Integer id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateAddress(Address address){
        return super.updateById(address);
    }

    @Override
    public Address getById(Integer id){
        return super.getById(id);
    }

    @Override
    public List<Address> selectList(Address address){
        return super.list(getQueryWrapper(address));
    }

    @Override
    public Page<Address> selectPage(Page<Address> page, Address address){
        return super.page(page, getQueryWrapper(address));
    }

    public LambdaQueryWrapper<Address> getQueryWrapper(Address address){
        return Wrappers.<Address>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(address.getId()), Address::getId, address.getId())
            .eq(ObjectUtils.isNotEmpty(address.getUsername()), Address::getUsername, address.getUsername())
            .eq(ObjectUtils.isNotEmpty(address.getProvinceid()), Address::getProvinceid, address.getProvinceid())
            .eq(ObjectUtils.isNotEmpty(address.getCityid()), Address::getCityid, address.getCityid())
            .eq(ObjectUtils.isNotEmpty(address.getAreaid()), Address::getAreaid, address.getAreaid())
            .eq(ObjectUtils.isNotEmpty(address.getPhone()), Address::getPhone, address.getPhone())
            .eq(ObjectUtils.isNotEmpty(address.getAddress()), Address::getAddress, address.getAddress())
            .eq(ObjectUtils.isNotEmpty(address.getContact()), Address::getContact, address.getContact())
            .eq(ObjectUtils.isNotEmpty(address.getIsDefault()), Address::getIsDefault, address.getIsDefault())
        ;
    }
}
